<?php
namespace Xiaoc\Api;
/**
 * Xiaoc试用活动
 *
 * @author: whs
 */
class TryGoods extends Common{
    public function getRules(){
        $data['TryList'] =array(
            'goods_name'    =>  array('name'=>'act_name','desc'=>'活动名称'),
            'cat_id'        =>  array('name'=>'cat_id','desc'=>'分类ID','type'=>'int'),
            'page'          =>  array('name'=>'page','desc'=>'页数','default'=>1,'type'=>'int'),
        );
        $data['TryInfo'] = array(
            'act_id'        =>  array('name'=>'act_id','desc'=>'活动ID','type'=>'int'),
            'ad_link'       =>  array('name'=>'ad_link','desc'=>'广告ID','type'=>'int'),
            'user_token'    =>  array('name'=>'user_token','desc'=>'用户TOKEN')
        );
        $data['apply_try'] = array(
            'act_id'        =>  array('name'=>'act_id','desc'=>'活动ID','require' => true,'type'=>'int'),
            'user_token'    =>  array('name'=>'user_token','desc'=>'用户TOKEN','require' => true),
            'address_id'    =>  array('name'=>'address_id','desc'=>'地址ID','require' => true,'type'=>'int'),
        );
        $data['add_read_num'] = array(
            'apply_id'        =>  array('name'=>'apply_id','desc'=>'申请ID','require' => true,'type'=>'int'),
            'access_token'    =>  array('name'=>'access_token','desc'=>'微信用户的TOKEN','require' => true),
            'openid'          =>  array('name'=>'openid','desc'=>'微信用户的OPENDID','require' => true),
        );
        $data['down_img'] = array(
            'server_id'           =>  array('name'=>'server_id','desc'=>'素材ID','require' => true),
            'access_token'        =>  array('name'=>'access_token','desc'=>'TOKEN','require' => true),
        );
        return $data;
    }
    /**
     * 试用活动列表
     * @desc    试用活动列表
     * @return string goods_name 活动名称
     * @return int end_time 结束时间
     * @return int act_id 活动ID
     * @return float market_price 市场价
     * @return string goods_thumb 商品缩略图
     * @return int total_num 限量数
     */
    public function TryList(){
        $join['[>]goods'] = array('goods_id'=>'goods_id');
        $where['goods_activity.act_type'] =GAT_TRY_GOODS;
        $where['goods_activity.review_status'] = ACTSUCCESS;
        $where['goods_activity.start_time[<]'] =gmtime();
        $where['goods_activity.end_time[>]'] =gmtime();
        if ($this->goods_name){
            $where['goods_activity.act_name[~]'] =trim($this->goods_name);
            $count_where['goods_activity.act_name[~]'] =$where['goods_activity.act_name[~]'];
        }
        if (intval($this->cat_id)){
            $where['goods.cat_id'] = intval($this->cat_id);
            $count_where['goods_activity.review_status'] = $where['goods.cat_id'];
        }
        $where['LIMIT']=array($this->limitStart,$this->limitEnd);
        $where['ORDER'] = array('goods_activity.act_id'=>'DESC');
        $field = array('goods_activity.goods_name','goods_activity.ext_info','goods_activity.end_time','goods_activity.act_id','goods.market_price','goods.goods_thumb');
        $list =$this->medoo->select('goods_activity',$join,$field,$where);
        $count = 0;
        if ($list){
            foreach ($list as $key=>$value){
                $res = unserialize($value['ext_info']);
                $list[$key]['total_num'] = $res['total_num'];
                unset($list[$key]['ext_info']);
            }
            $count_where['goods_activity.act_type'] =$where['goods_activity.act_type'];
            $count_where['goods_activity.review_status'] = $where['goods_activity.review_status'];
            $count = $this->medoo->count('goods_activity',$join,'goods_activity.act_id',$count_where);
            $data['list'] = $list;
        }else{
            $data['list'] = array();
        }
        $data['hasmore'] = $this->HasMore($count);
        return $data;
    }
    /**
     *热门试用活动列表(三条记录)
     * @desc    热门试用活动列表
     * @return string goods_name 活动名称
     * @return int end_time 结束时间
     * @return int act_id 活动ID
     * @return float market_price 市场价
     * @return string goods_thumb 商品缩略图
     * @return int num 剩余份数
     */
    public function HotTry(){
        $join['[>]goods'] = array('goods_id');
        $join['[>]try_apply'] = array('act_id');
        $where['goods_activity.act_type'] =GAT_TRY_GOODS;
        $where['goods_activity.review_status'] = ACTSUCCESS;
        $where['goods_activity.is_hot'] = 1;
        $where['goods_activity.start_time[<]'] =gmtime();
        $where['goods_activity.end_time[>]'] =gmtime();
        $where['GROUP'] = array('try_apply.act_id');
        $where['LIMIT']=3;
        $where['ORDER'] = array('goods_activity.act_id'=>'DESC');
        $field = array('goods_activity.goods_name','goods_activity.act_id','goods_activity.ext_info','goods_activity.end_time','goods_activity.act_id','goods.market_price','goods.goods_thumb','#COUNT(try_apply.act_id)(num)');
        $data = $this->medoo->select('goods_activity',$join,$field,$where);
        $list = array();
        if ($data){
            foreach ($data as $key=>$value){
                $info = unserialize($value['ext_info']);
                $data[$key]['num'] = intval($info['total_num']-$value['num']);
                unset($data[$key]['ext_info']);
            }
            $list = $data;
        }
        unset($data);
        return $list;
    }

    /**
     *试用活动详情
     * @desc    试用活动详情
     * @return string goods_name 活动名称
     * @return int end_time 结束时间
     * @return int goods_id 商品ID
     * @return float market_price 市场价
     * @return string goods_thumb 商品缩略图
     * @return int num 已申请人数
     * @return int total_num 限量数
     * @return int gift_num 赠送份数
     * @return boolean is_apply true为已申请
     * @return array goods_image 商品图片 thumb_url图片地址
     * @return  boolean is_collect 是否收藏
     */
    public function TryInfo(){
        $ad_link = $this->ad_link;
        $act_id = $this->act_id;
        if (empty($ad_link) && empty($act_id)){
            return array();
        }
        if ($act_id){
            $where['goods_activity.act_id'] =$act_id;
        }elseif ($ad_link){
            $where['goods_activity.goods_id'] =$ad_link;
        }
        $join['[>]goods'] = array('goods_id');
        $join['[>]try_apply'] = array('act_id');
        $where['goods_activity.act_type'] =GAT_TRY_GOODS;
        $where['goods_activity.review_status'] = ACTSUCCESS;
        $where['goods_activity.start_time[<]'] =gmtime();
        $where['goods_activity.end_time[>]'] =gmtime();
        $where['GROUP'] = array('try_apply.act_id');
        $field = array('goods_activity.goods_name','goods_activity.ext_info','goods_activity.end_time','goods_activity.goods_id','goods.market_price','#COUNT(try_apply.act_id)(num)','goods_activity.is_finished');
        $info = $this->medoo->get('goods_activity',$join,$field,$where);
        if ($info){
            if ($info['is_finished']==0){
                $this->medoo->update('goods_activity',array('is_finished'=>1),array('act_id'=>$act_id));
            }
            $info['is_apply'] = false;
            $info['is_collect'] = false;
            if ($this->user_token){
                $user_info = $this->check_login($this->user_token);
                $apply_id = $this->medoo->get('try_apply','apply_id',array('user_id'=>$user_info['user_id'],'act_id'=>$act_id));
                if ($apply_id){
                    $info['is_apply'] = true;
                }
                $rec_id =  $this->medoo->get('collect_goods','rec_id',array('user_id'=>$user_info['user_id'],'goods_id'=>$info['goods_id']));
                if ($rec_id){
                    $info['is_collect'] = true;
                }
            }
            //获取商品图片
            $info['goods_image'] = $this->medoo->select('goods_gallery','thumb_url',array('goods_id'=>$info['goods_id']));
            $serialize = unserialize($info['ext_info']);
            $info['total_num'] = $serialize['total_num'];
            $info['gift_num'] = $serialize['gift_num'];
            unset($info['ext_info']);
        }else{
            $info = array();
        }
        return $info;
    }
    /**
     *今日试用活动列表(一条记录)
     * @desc    今日试用活动列表
     * @return string goods_name 活动名称
     * @return int end_time 结束时间
     * @return int act_id 活动ID
     * @return float market_price 市场价
     * @return string goods_thumb 商品缩略图
     * @return int num 剩余份数
     */
    public function NewTry(){
        $join['[>]goods'] = array('goods_id');
        $join['[>]try_apply'] = array('act_id');
        $where['goods_activity.act_type'] =GAT_TRY_GOODS;
        $where['goods_activity.review_status'] = ACTSUCCESS;
        $where['goods_activity.is_hot'] = 1;
        $where['GROUP'] = array('try_apply.act_id');
        $where['LIMIT']=1;
        $where['goods_activity.start_time[<]'] =gmtime();
        $where['goods_activity.end_time[>]'] =gmtime();
        $where['ORDER'] = array('goods_activity.act_id'=>'DESC');
        $field = array('goods_activity.goods_name','goods_activity.act_id','goods_activity.ext_info','goods_activity.end_time','goods_activity.act_id','goods.market_price','goods.goods_thumb','#COUNT(try_apply.act_id)(num)');
        $data = $this->medoo->select('goods_activity',$join,$field,$where);
        $list = array();
        if ($data){
            foreach ($data as $key=>$value){
                $info = unserialize($value['ext_info']);
                $data[$key]['num'] = intval($info['total_num']-$value['num']);
                unset($data[$key]['ext_info']);
            }
            $list = $data;
        }
        unset($data);
        return $list;
    }

    /**
     * 获取所有的地区
     * @desc获取所有的地区
     * @return int id 地区ID
     * @return array child 子集
     * @return string name 地区名称
     */
    public function AreaList(){
        $areaList = $this->di->cache->get('areaList');
        if (empty($areaList)){
            $where['parent_id[!]'] = 0;
            $field = array('region_id(id)','parent_id(pid)','region_name(name)');
            $list = $this->medoo->select('region',$field,$where);
            foreach ($list as $value){
                $area_ids[] = $value['id'];
            }
            $this->di->cache->set('area_ids',$area_ids,86400*365);
            $areaList = $this->AreaLayer($list);
            $this->di->cache->set('areaList',$areaList,'86400*365');
        }
        return $areaList;
    }
    /**
     * 申请试用
     * @desc申请试用
     * @return int ret 200 表示成功
     */
    public function apply_try(){
        $user_info = $this->check_login($this->user_token);
        $act_id = $this->act_id;
        $address_id = $this->address_id;
        $join['[>]goods'] = array('goods_id');
        $join['[>]try_apply'] = array('act_id');
        $where['goods_activity.act_type'] =GAT_TRY_GOODS;
        $where['goods_activity.act_id'] =$act_id;
        $where['goods_activity.review_status'] = ACTSUCCESS;
        $where['goods_activity.start_time[<]'] =gmtime();
        $where['goods_activity.end_time[>]'] =gmtime();
        $where['GROUP'] = array('try_apply.act_id');
        $field = array('goods_activity.ext_info','goods_activity.goods_id','#COUNT(try_apply.act_id)(num)','goods_activity.is_finished','goods_activity.goods_name','goods.goods_thumb');
        $info = $this->medoo->get('goods_activity',$join,$field,$where);
        if ($info && $info['is_finished']==1){
            $serialize = unserialize($info['ext_info']);
            $total_num = $serialize['total_num'];
            $apply_id = $this->medoo->get('try_apply','apply_id',array('user_id'=>$user_info['user_id'],'act_id'=>$act_id));
            if ($info['num']>=intval($total_num))$this->api_error_msg('该活动申请数量已满');
            if (!$apply_id){
                $field = array('province','city','district','address','mobile','address_name');
                $address_info = $this->medoo->get('user_address',$field,array('address_id'=>$address_id,'user_id'=>$user_info['user_id']));
                if (!$address_info)$this->api_error_msg('请选择正确的收货地址');
                $arr['user_id'] = $user_info['user_id'];
                $arr['user_name'] = $user_info['user_name'];
                $arr['goods_id'] = $info['goods_id'];
                $arr['act_id'] = $act_id;
                $arr['country'] = 1;
                $arr['province'] = $address_info['province'];
                $arr['city'] = $address_info['city'];
                $arr['district'] = $address_info['district'];
                $arr['address'] = $address_info['address'];
                $arr['mobile'] = $address_info['mobile'];
                $arr['name'] = $address_info['address_name'];
                $arr['addtime'] = gmtime();
                $apply_id = $this->medoo->insert('try_apply',$arr);
                if (!$apply_id) $this->api_success_msg('申请失败');
            }
            $data['url'] = '/wap/template/free-detail/detail.html?act_id='.$act_id.'&apply_id='.$apply_id;
            $data['title'] = $info['goods_name'];
            $data['img'] = $info['goods_thumb'];
            return $data;
        }
        $this->api_error_msg('该活动您不存在');
    }
    /**
     * 统计阅读量
     * @desc APP忽略
     * @return boolean status
     */
    public function add_read_num(){
        $apply_id = $this->apply_id;
        $openid = $this->openid;
        $access_token = $this->access_token;
        $obj = new \Wx_Lite();
        $user_info = $obj->getUserInfo($access_token,$openid);
        if ($user_info['status'] && $user_info['data']){
            $user_info = $user_info['data'];
            $where['open_id'] = trim($user_info['openid']);
            $where['apply_id'] = $apply_id;
            $log = $this->medoo->get('try_apply_log','log_id',$where);
            if (empty($log)){
                $arr['apply_id'] = $apply_id;
                $arr['open_id'] = $user_info['openid'];
                $arr['addtime'] = gmtime();
                $this->medoo->insert($arr);
            }
            return $data['status'] = true;
        }

    }
    public function down_img(){
        $server_id = $this->server_id;
        $access_token = $this->access_token;
        $url = 'https://api.weixin.qq.com/cgi-bin/media/get?access_token='.$access_token.'&media_id='.$server_id;
        $this->di->cache->set('url',$url,60*3600);
        $res = getUrl($url);
        return $res;
    }
}